require "proto/arena/message/ReqArenaBattleMessage"
require "proto/arena/message/ReqSetDefFormationMessage"
require "proto/arena/message/ReqDrawArenaBoxMessage"
require "proto/arena/message/ReqBuyTicketMessage"
require "proto/arena/message/ReqArenaAddSharedRecordMessage"
require "proto/arena/message/ReqArenaBattleRecordsMessage"
require "proto/arena/message/ReqArenaGetRanksMessage"
require "proto/arena/message/ReqArenaOpponentMessage"
require "proto/arena/message/ReqArenaSharedRecordsMessage"
require "proto/arena/message/ReqArenaInfoMessage"

arenaHandler = arenaHandler or {};
G.serverData = G.serverData or {};

function arenaHandler.receiveArenaInfo(msg)
    --log("arenaHandler.receiveArenaInfo");
    if msg.state == 0 then
        logicMgr.ArenaManager.setBaseInfo(msg);
        FireEvent(Config.EventType.Fresh_Arena);
    end
end

--- @param msg ResArenaBattleResultMessage 挑战结果返回
function arenaHandler.receiveBattleResult(msg)
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end
    logicMgr.ArenaManager.refreshBattleInfo(msg);
    FightManager.InitRewards(msg.items)
    BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)
    if ArenaManager.getSomeBattleSkip() then
        FightManager.showResultUI(true);
        closeUI(PanelNames.UIBattleTeam)
        return
    end
    --[[if msg.state == 0 then
        logicMgr.ArenaManager.refreshBattleInfo(msg);
        BattlePlaybackManager.setBattleRecord(msg.record);
        BattlePlaybackManager.setBattleResult(msg.fightResult)
        BattlePlaybackManager.requestFightCmdThenPlay(msg.record, EnumConst.FightTypeConst.ARENA, function()
            closeUI(PanelNames.UIArenaMain);
            closeUI(PanelNames.UIAdventureMain);
            closeUI(PanelNames.UIBottom);
            closeUI(PanelResNames.UIHeadMenu);
        end, function()
            showUI(PanelNames.UIArenaMain);
        end);
    end]]
end

function arenaHandler.receiveDefFormation(msg)
    --log("arenaHandler.receiveDefFormation");
    if msg.state == 0 then
        local formations = {};
        for i = 1, #msg.formation do
            table.insert(formations, tostring(msg.formation[i]));
        end
        logicMgr.FormationManager.SetFormation(EnumConst.FormationType.ArenaDef, formations);
        FireEvent(Config.EventType.Fresh_ArenaFormation);
    end
end

function arenaHandler.receiveDrawArenaBox(msg)
    --log("arenaHandler.receiveDrawArenaBox");
    if msg.state == 0 then
        logicMgr.ArenaManager.addRewardBox(msg.boxId);
        local rewardsCfg = decodeJsonStrToTabel(G.dataTable["t_arenaboxreward"][tostring(msg.boxId)].f_rewards);
        local rewards = {};
        for k, v in pairs(rewardsCfg) do
            local item = {};
            item.num = v[2];
            item.itemModelId = v[1];
            --    local item = logicMgr.ItemManager.getItemData(v[1]);
            table.insert(rewards, item);
        end
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards)
        FireEvent(Config.EventType.Fresh_Arena);
    end
end

function arenaHandler.receiveBuyTicket(msg)
    --log("arenaHandler.receiveBuyTicket");
    if msg.state == 0 then
        logicMgr.ArenaManager.addBuyTicketCount(msg.num);
        FireEvent(Config.EventType.Fresh_ArenaTicket);
    end
end

--- process msg
--- @param msg ResArenaAddSharedRecordMessage 竞技场增加分享记录请求
function arenaHandler.receiveAddSharedRecord(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.onAddSharedSuccess(msg.fightId);
    end
end

--- process msg
--- @param msg ResArenaBattleRecordsMessage 竞技场战斗记录请求
function arenaHandler.receiveArenaBattleRecords(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.setRecordInfo(msg.recordVersion, msg.records);
        FireEvent(Config.EventType.Fresh_ArenaRecords);
    end
end

--- process msg
--- @param msg ResArenaOpponentMessage 竞技场匹配对战列表消息
function arenaHandler.receiveOpponentInfo(msg)
    if msg.state == 0 then
        ArenaManager.setOpponentInfo(msg);
        FireEvent(Config.EventType.Fresh_Arena);
    end
end

--- process msg
--- @param msg ResArenaSharedRecordsMessage 竞技场分享战报列表消息
function arenaHandler.receiveArenaSharedRecords(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.setSharedRecords(msg);
        FireEvent(Config.EventType.Fresh_ArenaSharedRecords);
    end
end

--- process msg
--- @param msg ResArenaGetRanksMessage msg
function arenaHandler.receiveArenaGetRanks(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.setRankInfo(msg);
        FireEvent(Config.EventType.Fresh_ArenaRanks);
    end
end

function arenaHandler.sendArenaBattle(foeId, isAi, formation, fightId)
    local req = ReqArenaBattleMessage.New();
    req.foeId = foeId;
    req.isAi = isAi;
    req.formation = formation;
    req.version = Config.ProtoVersion;
    if fightId ~= nil then
        req.fightId = fightId;
    else
        req.fightId = 0;
    end
    sendReqMessage(req);
end

function arenaHandler.sendDefFormation(formation)
    local req = ReqSetDefFormationMessage.New();
    req.formation = formation;
    sendReqMessage(req);
end

function arenaHandler.sendDrawArenaBox(boxId)
    local req = ReqDrawArenaBoxMessage.New();
    req.boxId = boxId;
    sendReqMessage(req);
end

function arenaHandler.sendBuyTicket(count)
    local req = ReqBuyTicketMessage.New();
    req.num = count;
    sendReqMessage(req);
end

--- 发送竞技场记录请求
--- @param localRecordVer number 本地版本号
function arenaHandler.sendArenaBattleRecords(localRecordVer)
    local req = ReqArenaBattleRecordsMessage.New();
    req.recordVersion = localRecordVer;
    sendReqMessage(req);
end

--- 发送挑战列表请求
function arenaHandler.sendArenaOpponentMessage()
    local req = ReqArenaOpponentMessage.New();
    sendReqMessage(req);
end

--- 发送分享战报列表请求
function arenaHandler.sendArenaSharedRecords()
    local req = ReqArenaSharedRecordsMessage.New();
    sendReqMessage(req);
end

--- 发送竞技场排行榜请求
--- @param rankVersion number 排行榜版本号
function arenaHandler.sendArenaGetRanks(rankVersion)
    local req = ReqArenaGetRanksMessage.New()
    req.rankVersion = rankVersion
    sendReqMessage(req)
end

--发送竞技场信息请求
function arenaHandler.sendReqArenaInfo()
    local req = ReqArenaInfoMessage.New()
    sendReqMessage(req)
end
